/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.loja.model.entity.conta;

import br.com.loja.model.entity.Switchable;
import br.com.loja.view.swing.model.FormField;
import br.com.loja.view.swing.model.IgnoreTableModel;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

/**
 *
 * @author David Luiz
 */
@Entity
@Table(name = "conta")
public class Conta implements Switchable, Serializable {

    public static final long serialVersionUID = 1L;

    @Id
    private String idConta;
    private String descricao;
    @Transient
    private BigDecimal saldo;
    private BigDecimal saldoBloqueado = BigDecimal.ZERO;
    @Transient
    private BigDecimal saldoDisponivel;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "conta", fetch = FetchType.EAGER)
    @IgnoreTableModel
    private List<Operacao> operacoes = new ArrayList<>();

    @FormField(visible = false)
    @IgnoreTableModel
    private boolean dead;

    public Conta() {
    }

    /**
     * @return the idConta
     */
    public String getIdConta() {
        return idConta;
    }

    /**
     * @param idConta the idConta to set
     */
    public void setIdConta(String idConta) {
        this.idConta = idConta;
    }

    /**
     * @return the descricao
     */
    public String getDescricao() {
        return descricao;
    }

    /**
     * @param descricao the descricao to set
     */
    public void setDescricao(String descricao) {
        this.descricao = descricao;
    }

    /**
     * @return the saldoBloqueado
     */
    public BigDecimal getSaldoBloqueado() {
        return saldoBloqueado;
    }

    /**
     * @param saldoBloqueado the saldoBloqueado to set
     */
    public void setSaldoBloqueado(BigDecimal saldoBloqueado) {
        this.saldoBloqueado = saldoBloqueado;
    }

    /**
     * @return the operacoes
     */
    public List<Operacao> getOperacoes() {
        return operacoes;
    }

    /**
     * @param operacoes the operacoes to set
     */
    public void setOperacoes(List<Operacao> operacoes) {
        this.operacoes = operacoes;
    }

    /**
     * @return the dead
     */
    public boolean isDead() {
        return dead;
    }

    /**
     * @param dead the dead to set
     */
    public void setDead(boolean dead) {
        this.dead = dead;
    }

    @Override
    public String toString() {
        return this.getDescricao();
    }

    public BigDecimal getSaldo() {
        BigDecimal total = BigDecimal.ZERO;

        for (Operacao operacao : operacoes) {
            total = total.add(operacao.getValor());
        }

        return total;
    }

    public void setSaldo(BigDecimal saldo) {
    }

    public BigDecimal getSaldoDisponivel() {
        return getSaldo().subtract(saldoBloqueado);
    }

    public void setSaldoDisponivel(BigDecimal saldoDisponivel) {
    }
}
